package cn.tedu.boot33.controller;

import cn.tedu.boot33.mapper.ItemMapper;
import cn.tedu.boot33.vo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {
    @Autowired(required = false)
    ItemMapper mapper;

    @RequestMapping("/insert")
    public String insert(Item item) {
        mapper.insert(item);
        return "添加完成<a href='/'>回到首页</a>";
    }

    @RequestMapping("/select")
    public String select() {
        List<Item> list = mapper.select();
        String html = "";
        for (Item i : list) {
            html += "<div>";
            html += "<h2>" + i.getId() + "</h2>";
            html += "<h3>" + i.getName() + "</h3>";
            html += "<p>" + i.getNum() + "</p>";
            html += "<p>" + i.getPrice() + "</p>";
            html += "<p>" + i.getIntro() + "</p>";
            html += "<a href='/delete?id=" + i.getId() + "'>删除</a>";
            html += "</div>";
        }
        return html;
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        mapper.delete(id);
        return "删除成功<a href='/select'>返回用户列表</a>";
    }

    @RequestMapping("/update")
    public String update(Item item){
        mapper.update(item);
        return "添加成功<a href='/select'>返回用户列表</a>";
    }
}
